/**
 * 20080124 1750: Eric Kao was created.<br>
 */

package index;

import info.Info;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import pre.AbstractPreTable;
import pre.PreTestTable;

import com.e885.exception.E885DBException;
import com.e885.jdbc.JDBCUtil;
import com.e885.jdbc.OracleJDBC;

/**
 * 測試 Result.absolute() 的速度以及使用到的記憶體<br>
 * 
 * Copyright: Copyright (c) 2002-2007<br>
 * Company: E885 工作室<br>
 * 
 * @author Eric Gau E-mail: gau168.gau@msa.hinet.net
 * @version 1.0
 */

public class SetAbsoluteResource
{
	private String ip = Info.IP;

	private int port = Info.PORT;

	private String userName = Info.USER_NAME;

	private String password = Info.PASSWORD;

	private String databaseName = Info.DATABASE_NAME;

	private Connection connection;

	private int rowCount = 5000;

	public SetAbsoluteResource()
		throws E885DBException, SQLException
	{
		long startTime;
		long endTime;

		long memBefore;
		long memAfter;

		Runtime runtime = Runtime.getRuntime();

		JDBCUtil jdbcUtil = new OracleJDBC( ip, port, userName, password, databaseName );

		connection = jdbcUtil.getConnection();

		AbstractPreTable testTable = new PreTestTable( connection );

		testTable.createTable();

		testTable.insertData( rowCount );

		ResultSet rs = testTable.selectData();

		// move cursot to last
		memBefore = runtime.freeMemory();
		startTime = System.currentTimeMillis();

		rs.absolute( rowCount - 1 );

		endTime = System.currentTimeMillis();
		memAfter = runtime.freeMemory();

		System.out.println( "Move cursor to use time: " + ( endTime - startTime ) );
		System.out.println( "Move cursor to use memory: " + ( memBefore - memAfter ) );

		System.out.println( "id:" + rs.getString( PreTestTable.ID_CONSTANT ) );
		System.out.println( "name:" + rs.getString( PreTestTable.NAME_CONSTANT ) );
		System.out.println( "address:" + rs.getString( PreTestTable.ADDRESS_CONSTANT ) );

		testTable.dropTable();
	}

	public static void main( String[] args )
	{
		try
		{
			new SetAbsoluteResource();
		}
		catch( Exception ex )
		{
			ex.printStackTrace();
		}
	}
}
